package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.enums.IgasYesOrNoEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.igas.dao.PSecPlanInfoMapper;
import cc.rengu.redp.bizimpl.igas.domain.*;
import cc.rengu.redp.bizimpl.igas.domain.PSecPlanInfo;
import cc.rengu.redp.bizimpl.igas.service.PCertInfoService;
import cc.rengu.redp.bizimpl.igas.service.PKeyInfoService;
import cc.rengu.redp.bizimpl.igas.service.PSecPlanInfoService;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpPrivilegeCheck;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 刘玉亮.LL
 */
@Service
@DS("igas")
public class PSecPlanInfoServiceImpl extends ServiceImpl<PSecPlanInfoMapper, PSecPlanInfo> implements PSecPlanInfoService {

    @Autowired
    IUpmpSysParamService upmpSysParamService;
    @Autowired
    private PKeyInfoService pKeyInfoService;
    @Autowired
    private PCertInfoService pCertInfoService;

    @Override
    public IPage<PSecPlanInfo> findByPage(QueryRequest request, PSecPlanInfo info) {
        try {
            LambdaQueryWrapper<PSecPlanInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getInstId())) {
                queryWrapper.eq(PSecPlanInfo::getInstId, info.getInstId());
            } else {
                //获取当前用户的法人id
                UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
                queryWrapper.eq(PSecPlanInfo::getInstId, user.getInstId());
            }
            if (StringUtils.isNotBlank(info.getSecPlanId())) {
                queryWrapper.likeRight(PSecPlanInfo::getSecPlanId, info.getSecPlanId());
            }
            if (StringUtils.isNotBlank(info.getSecPlanType())) {
                queryWrapper.likeRight(PSecPlanInfo::getSecPlanType, info.getSecPlanType());
            }
            queryWrapper.orderByDesc(PSecPlanInfo::getSecPlanType);
            Page<PSecPlanInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    public PSecPlanInfo findOne (String instId,String  secPlanId, String  secPlanType){
        LambdaQueryWrapper<PSecPlanInfo> wraper = new LambdaQueryWrapper<>();
        wraper.eq(PSecPlanInfo::getInstId,instId);
        wraper.eq(PSecPlanInfo::getSecPlanId, secPlanId);
        wraper.eq(PSecPlanInfo::getSecPlanType, secPlanType);
        PSecPlanInfo info = this.getOne(wraper);
        return info;
    }
    @Override
    public void saveData(PSecPlanInfo pSecPlanInfo, PKeyInfo pKeyInfo, PCertInfo pCertInfo)throws RedpException{
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        if(PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(pSecPlanInfo.getOptFlag())){//新增
            pSecPlanInfo.setSecPlanType(pSecPlanInfo.getKeyType()+pSecPlanInfo.getKeyOprType()+pSecPlanInfo.getKeyUsage());
            pSecPlanInfo.setCreateTime(lastUpddt);
            PSecPlanInfo entity = findOne(pSecPlanInfo.getInstId(),pSecPlanInfo.getSecPlanId(),pSecPlanInfo.getSecPlanType());
            if(Common.isBlank(entity)){
                pSecPlanInfo.setCreateTime(lastUpddt);
                this.save(pSecPlanInfo);
            }else {
                pSecPlanInfo.setUpdateTime(lastUpddt);
                LambdaUpdateWrapper<PSecPlanInfo> updateWrapper= new LambdaUpdateWrapper<>();
                updateWrapper.ge(PSecPlanInfo::getSecPlanId,pSecPlanInfo.getSecPlanId());
                updateWrapper.ge(PSecPlanInfo::getSecPlanType,pSecPlanInfo.getSecPlanType());
                this.update(pSecPlanInfo,updateWrapper);
            }
            if("C".equals(pSecPlanInfo.getKeyType())){
                pCertInfo.setCertIndex(pSecPlanInfo.getInstId()+pSecPlanInfo.getSecPlanId()+pSecPlanInfo.getSecPlanType());
                PCertInfo db  = pCertInfoService.getById(pCertInfo.getCertIndex());
                if(Common.isNotBlank(db)){
                    throw new RedpException("安全计划对应的证书已经存在");
                }

                pCertInfo.setCreateTime(lastUpddt);
                pCertInfoService.saveOrUpdate(pCertInfo);
            }else if("K".equals(pSecPlanInfo.getKeyType())){
                pKeyInfo.setKeyIndex(pSecPlanInfo.getInstId()+pSecPlanInfo.getSecPlanId()+pSecPlanInfo.getSecPlanType());

                PKeyInfo db  = pKeyInfoService.getById(pKeyInfo.getKeyIndex());
                if(Common.isNotBlank(db)){
                    throw new RedpException("安全计划对应的密钥信息已经存在！");
                }
                pKeyInfo.setCreateTime(lastUpddt);
                pKeyInfoService.saveOrUpdate(pKeyInfo);
            }
        }else if(PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(pSecPlanInfo.getOptFlag())){
            pSecPlanInfo.setSecPlanType(pSecPlanInfo.getKeyType()+pSecPlanInfo.getKeyOprType()+pSecPlanInfo.getKeyUsage());
            PSecPlanInfo entity = findOne(pSecPlanInfo.getInstId(),pSecPlanInfo.getSecPlanId(),pSecPlanInfo.getSecPlanType());
            if(Common.isBlank(entity)){
                pSecPlanInfo.setCreateTime(lastUpddt);
                this.save(pSecPlanInfo);
            }else {
                pSecPlanInfo.setUpdateTime(lastUpddt);
                LambdaUpdateWrapper<PSecPlanInfo> updateWrapper= new LambdaUpdateWrapper<>();
                updateWrapper.ge(PSecPlanInfo::getSecPlanId,pSecPlanInfo.getSecPlanId());
                updateWrapper.ge(PSecPlanInfo::getSecPlanType,pSecPlanInfo.getSecPlanType());
                this.update(pSecPlanInfo,updateWrapper);
            }

            if("C".equals(pSecPlanInfo.getKeyType())){
                PCertInfo pc = pCertInfoService.getById(pSecPlanInfo.getInstId()+pSecPlanInfo.getSecPlanId()+pSecPlanInfo.getSecPlanType());
                if(Common.isBlank(pc)){
                    pCertInfo.setCertIndex(pSecPlanInfo.getInstId()+pSecPlanInfo.getSecPlanId()+pSecPlanInfo.getSecPlanType());
                    PCertInfo db  = pCertInfoService.getById(pCertInfo.getCertIndex());
                    if(Common.isNotBlank(db)){
                        throw new RedpException("安全计划对应的证书已经存在");
                    }
                    pCertInfo.setCreateTime(lastUpddt);
                    pCertInfoService.saveOrUpdate(pCertInfo);
                }else{
                    BeanUtils.copyProperties(pCertInfo,pc);
                    pc.setUpdateTime(lastUpddt);
                    pCertInfoService.saveOrUpdate(pc);
                }
            }else if("K".equals(pSecPlanInfo.getKeyType())){

                PKeyInfo pk = pKeyInfoService.getById(pSecPlanInfo.getInstId()+pSecPlanInfo.getSecPlanId()+pSecPlanInfo.getSecPlanType());
                if(Common.isBlank(pk)){
                    pKeyInfo.setKeyIndex(pSecPlanInfo.getInstId()+pSecPlanInfo.getSecPlanId()+pSecPlanInfo.getSecPlanType());

                    PKeyInfo db  = pKeyInfoService.getById(pKeyInfo.getKeyIndex());
                    if(Common.isNotBlank(db)){
                        throw new RedpException("安全计划对应的密钥信息已经存在！");
                    }
                    pKeyInfo.setCreateTime(lastUpddt);
                    pKeyInfoService.saveOrUpdate(pKeyInfo);
                }else{
                    BeanUtils.copyProperties(pKeyInfo,pk);
                    pk.setUpdateTime(lastUpddt);
                    pKeyInfoService.saveOrUpdate(pk);
                }
            }
        }else if(PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(pSecPlanInfo.getOptFlag())
                || PageOptFlagEnum.OPT_FLAG_USE.getType().equals(pSecPlanInfo.getOptFlag())){
            PSecPlanInfo entity = findOne(pSecPlanInfo.getInstId(),pSecPlanInfo.getSecPlanId(),pSecPlanInfo.getSecPlanType());
            if(Common.isNotBlank(entity)){
                if(PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(pSecPlanInfo.getOptFlag())){
                    entity.setSecPlanStatus(IgasYesOrNoEnum.No.getType());
                }else {
                    entity.setSecPlanStatus(IgasYesOrNoEnum.Yes.getType());
                }
                if("C".equals(pSecPlanInfo.getKeyType())){
                    PCertInfo pc = pCertInfoService.getById(pSecPlanInfo.getInstId()+pSecPlanInfo.getSecPlanId()+pSecPlanInfo.getSecPlanType());
                    if(Common.isNotBlank(pc)){
                        pc.setCertStatus(entity.getSecPlanStatus());
                        pc.setUpdateTime(lastUpddt);
                        pCertInfoService.saveOrUpdate(pc);
                    }
                }else if("K".equals(pSecPlanInfo.getKeyType())){

                    PKeyInfo pk = pKeyInfoService.getById(pSecPlanInfo.getInstId()+pSecPlanInfo.getSecPlanId()+pSecPlanInfo.getSecPlanType());
                    if(Common.isNotBlank(pk)){
                        pk.setKeyStatus(entity.getSecPlanStatus());
                        pk.setUpdateTime(lastUpddt);
                        pKeyInfoService.saveOrUpdate(pk);
                    }
                }
            }
        }

    }
}
